﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Business;
using System.IO;
using Foundation;

namespace RPGSoundmixer {
    public partial class TaggingForm : Form {
        private DataHandler dataHandler; 
        

        public TaggingForm() {
            InitializeComponent();
            this.dataHandler = DataHandler.GetInstance();
            this.dataHandler.PropertyChanged += new PropertyChangedEventHandler(dataHandler_PropertyChanged);
            this.listCategories.Items.AddRange(dataHandler.Categories.ToArray());
            this.listUntaggedTracks.Items.AddRange(dataHandler.UntaggedTracks.ToArray()); 
        }

        /// <summary>
        /// Handler für den Property Change Event
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void dataHandler_PropertyChanged(object sender, PropertyChangedEventArgs e) {
            if (e.PropertyName.Equals("untaggedTracks")) {
                this.listUntaggedTracks.Items.Clear();
                this.listUntaggedTracks.Items.AddRange(dataHandler.UntaggedTracks.ToArray()); 
            }
            else {

                this.listCategories.Items.Clear();
                this.listCategories.Items.AddRange(dataHandler.Categories.ToArray());
            }
        }

        /// <summary>
        /// Handler für den Add Button
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void addButton_Click(object sender, EventArgs e) {
            if (!string.IsNullOrEmpty(this.txtCategoryName.Text)) {
                dataHandler.SaveNewCategory(this.txtCategoryName.Text);
                this.txtCategoryName.Clear();
            }
        }

        /// <summary>
        /// spielt einen ausgewählten Track ab
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void listUntaggedTracks_DoubleClick(object sender, EventArgs e) {
            ListBox lstBox = (ListBox)sender;
            if (lstBox.SelectedItem != null) {
                Track t = (Track)lstBox.SelectedItem;
                string path = Path.Combine(t.Path, t.Name);
                var playlist = this.axWindowsMediaPlayer1.newPlaylist("preview", path);
                playlist.appendItem(this.axWindowsMediaPlayer1.newMedia(path)  ); 
                this.axWindowsMediaPlayer1.currentPlaylist = playlist;
                this.axWindowsMediaPlayer1.Ctlcontrols.play();
            }
        }

        private void btTag_Click(object sender, EventArgs e) {
            foreach (var item in this.listCategories.SelectedItems) {
                Category c = (Category)item;
                c.Tracklist.Add((Track)this.listUntaggedTracks.SelectedItem);
                this.dataHandler.SaveCategory(c); 
            }
            
        }

        
    }
}
